<script setup lang="ts">
import useStore from '@/store'
import { CommentAddReq, CommentSource, CommentTreeDto } from '@/api/comment/type'
import { PropType } from 'vue'

const store = useStore()
defineProps({
  dataList: {
    type: Array<CommentTreeDto>,
    required: true,
    default: () => []
  },
  articleId: {
    type: Number,
    required: false,
    default: () => null
  },
  headValue: {
    type: String,
    default: () => '评论'
  },
  listValue: {
    type: String,
    default: () => '全部评论'
  },
  source: {
    type: String as PropType<CommentSource>,
    default: null
  }
})
const emit = defineEmits(['listComments', 'submitComments', 'deleteComments'])
function deleteComments(item: CommentTreeDto) {
  emit('deleteComments', item)
}
function listComments() {
  emit('listComments')
}
function submitComments(val: CommentAddReq) {
  emit('submitComments', val)
}
</script>

<template>
  <div>
    <h3 class="flex-center">{{ headValue }}</h3>
    <div class="comment-top">
      <div class="current-user-avatar">
        <img :src="store.avatar ? store.avatar : '../static/avatar.svg'" :alt="store.username" />
      </div>
      <div style="width: 100%">
        <!--评论输入-->
        <CommentInput
          :article-id="articleId"
          :source="source"
          @submit-comments="submitComments"
        ></CommentInput>
      </div>
    </div>
    <!--评论列表标题-->
    <h3 class="flex-center">{{ listValue }}</h3>
    <!--评论列表信息-->
    <CommentItem
      v-if="dataList && dataList.length > 0"
      :data-list="dataList"
      :article-id="articleId"
      :source="source"
      @list-comment="listComments"
      @submit-comment="submitComments"
      @delete-comment="deleteComments"
    />
  </div>
</template>

<style scoped lang="scss">
.comment-top {
  display: flex;
  justify-content: space-between;
}
.current-user-avatar {
  width: 40px;
  height: 40px;
  margin-right: 12px;
  img {
    width: 100%;
    border-radius: 50%;
  }
}
</style>
